//
//  ContactViewController.m
//  iWeddingDirectory
//
//  Created by Tung Ngo on 6/4/12.
//  Copyright (c) 2012 Home. All rights reserved.
//

#import "ContactViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "API.h"

@implementation ContactViewController
@synthesize textFieldName;
@synthesize textFieldEmail;
@synthesize textViewMessage;
@synthesize waitingIndicator;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Contact" image:[UIImage imageNamed:@"contact.png"] tag:0];
        self.tabBarItem = item;
        [item release];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"Contact";
    [waitingIndicator setHidden:YES];

    textViewMessage.layer.borderWidth = 1;
    textViewMessage.layer.borderColor = [UIColor blackColor].CGColor;
}

- (void)viewDidUnload
{
    [self setTextViewMessage:nil];
    [self setTextFieldName:nil];
    [self setTextFieldEmail:nil];
    [self setWaitingIndicator:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [textViewMessage release];
    [textFieldName release];
    [textFieldEmail release];
    [waitingIndicator release];
    [super dealloc];
}

#pragma mark - Text field delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - Text view delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    else {
        return YES;
    }
}

#pragma mark - Notification handlers

- (void)onContactUs:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kContactUsNotification object:nil];
    
    [waitingIndicator stopAnimating];
    
    NSDictionary *dictionary = [notification userInfo];
    NSString *status = [dictionary objectForKey:@"status"];
    if ([status isEqualToString:@"ok"]) {
        NSString *message = [dictionary objectForKey:@"success"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
}

#pragma mark - Button click handlers

- (IBAction)onButtonSendMessageClicked:(id)sender {
    if (textFieldName.text == nil || [textFieldName.text length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You must fill your name first" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    if (textFieldEmail.text == nil || [textFieldEmail.text length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You must fill your email first" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    if (textViewMessage.text == nil || [textViewMessage.text length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You must fill the message first" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    [waitingIndicator setHidesWhenStopped:YES];
    [waitingIndicator setHidden:NO];
    [waitingIndicator startAnimating];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onContactUs:) name:kContactUsNotification object:nil];
    
    API *api = [API sharedInstance];
    [api contactUsWithName:textFieldName.text message:textViewMessage.text email:textFieldEmail.text];
}

@end
